package gui;


import java.awt.Color;
import java.awt.Point;

import javax.swing.JFrame;

import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import java.awt.BorderLayout;

import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.JSlider;

import painter.BezierDrawable;
import painter.JCanvas;
import painter.JCanvasMouseEventListener;
import painter.LineDrawable;
import network.Connector;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.JScrollPane;

import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JPasswordField;
import javax.swing.border.LineBorder;

public class MainWindow {
	

	private boolean isRegistering = false;
	private boolean isSpectateur = false;
	
	private JFrame frmIsketch;
	private JTextField textField_tchat;
	private JTextField textField_game;
	private DefaultListModel<String> dlm_game;
	private DefaultListModel<String> dlm_tchat;
	private JButton btn_pass;
	private JButton btnCheatGame;
	private JCanvas panel_draw_in_out;
	
	private JRadioButton rdbtn_line;
	private JRadioButton rdbtn_bezier;
	private JRadioButton rdbtn_color_noir;
	private JRadioButton rdbtn_color_bleu;
	private JRadioButton rdbtn_color_rouge;
	private JRadioButton rdbtn_color_vert;
	private JRadioButton rdbtn_color_jaune;
	private JRadioButton rdbtn_color_gris;
	private JLabel lbl_word_to_find;
	private JButton btnSendGame;
	private JButton btnSendTchat;
	
	private JCanvasMouseEventListener canvas_event_manager;
	
	private String currentPseudo;
	private boolean isDessinateur = false;
	private boolean gameStarted = false;
	private Color colorSelected = Color.BLACK;
	private final JSlider slider = new JSlider();
	
	private JTextField textField_IP;
	private JTextField textField_Port;
	private JTextField textField_create_identifiant;
	private JPasswordField passwordField;
	private JTextField textField_connexion_id;
	private JPasswordField passwordField_connexion;
	private JRadioButton rdbtn_connection_anon;
	private JRadioButton rdbtn_connection_account;
	private JLabel lblCreateMessage;
	
	private boolean isGameInterfaceLaunched = false;
	
	/* Champs accessible au mouse event listener */
	public Connector connector;
	


	
	public String getCurrentPseudo() {
		return currentPseudo;
	}
	
	public boolean currentPlayerIsDessinateur() {
		return isDessinateur;
	}
	
	public void setConnector(Connector connector) {
		
		if (connector != null) {
			this.connector = connector;
		}
	}



	
	
	/*********************** Fenêtre de connexion ***********************/
	
	public void initialize() {
		frmIsketch = new JFrame();
		frmIsketch.setVisible(true);
		frmIsketch.setTitle("iSketch");
		frmIsketch.setBounds(100, 100, 458, 382);
		frmIsketch.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				exitApplication();
			}
		});
		frmIsketch.getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel panel_nord = new JPanel();
		frmIsketch.getContentPane().add(panel_nord, BorderLayout.NORTH);
		panel_nord.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_parameters = new JPanel();
		panel_nord.add(panel_parameters, BorderLayout.NORTH);
		panel_parameters.setBorder(new TitledBorder(null, "Param\u00E8tres", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_parameters.setLayout(new GridLayout(1, 2, 0, 0));
		
		JPanel panel_ip = new JPanel();
		panel_parameters.add(panel_ip);
		panel_ip.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblIp = new JLabel("  IP      :    ");
		panel_ip.add(lblIp);
		
		textField_IP = new JTextField();
		textField_IP.setText("127.0.0.1");
		panel_ip.add(textField_IP);
		textField_IP.setColumns(10);
		
		JPanel panel_port = new JPanel();
		panel_parameters.add(panel_port);
		panel_port.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblPort = new JLabel("  Port  :    ");
		panel_port.add(lblPort);
		
		textField_Port = new JTextField();
		textField_Port.setText("2013");
		panel_port.add(textField_Port);
		textField_Port.setColumns(10);
		
		JPanel panel_account_creation = new JPanel();
		panel_nord.add(panel_account_creation, BorderLayout.SOUTH);
		panel_account_creation.setBorder(new TitledBorder(null, "Cr\u00E9er compte", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_account_creation.setLayout(new GridLayout(3, 1, 0, 0));
		
		JPanel panel_create_identifiant = new JPanel();
		panel_account_creation.add(panel_create_identifiant);
		panel_create_identifiant.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblIdentifiant = new JLabel("   Identifiant   :   ");
		panel_create_identifiant.add(lblIdentifiant);
		
		textField_create_identifiant = new JTextField();
		panel_create_identifiant.add(textField_create_identifiant);
		textField_create_identifiant.setColumns(10);
		
		JPanel panel_create_password = new JPanel();
		panel_account_creation.add(panel_create_password);
		panel_create_password.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblPassword = new JLabel("   Mot de passe   :   ");
		panel_create_password.add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setColumns(10);
		panel_create_password.add(passwordField);
		
		JPanel panel_create_valid = new JPanel();
		panel_account_creation.add(panel_create_valid);
		panel_create_valid.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_create_envoyer = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_create_envoyer.getLayout();
		flowLayout.setVgap(0);
		flowLayout.setHgap(0);
		panel_create_valid.add(panel_create_envoyer);
		
		JButton btnEnvoyer = new JButton("Envoyer");
		panel_create_envoyer.add(btnEnvoyer);
		btnEnvoyer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnCreateAccountClicked(textField_IP.getText(), textField_Port.getText(), textField_create_identifiant.getText(), new String(passwordField.getPassword()));
			}
		});
		
		JPanel panel_create_message = new JPanel();
		panel_create_valid.add(panel_create_message);
		
		lblCreateMessage = new JLabel("");
		panel_create_message.add(lblCreateMessage);
		
		JPanel panel_sud = new JPanel();
		frmIsketch.getContentPane().add(panel_sud, BorderLayout.SOUTH);
		panel_sud.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_connection = new JPanel();
		panel_connection.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Connexion", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_sud.add(panel_connection, BorderLayout.SOUTH);
		panel_connection.setLayout(new GridLayout(4, 1, 0, 0));
		
		JPanel panel_connection_id = new JPanel();
		panel_connection.add(panel_connection_id);
		panel_connection_id.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel label = new JLabel("   Identifiant   :   ");
		panel_connection_id.add(label);
		
		textField_connexion_id = new JTextField();
		textField_connexion_id.setColumns(10);
		panel_connection_id.add(textField_connexion_id);
		
		JPanel panel_connection_password = new JPanel();
		panel_connection.add(panel_connection_password);
		panel_connection_password.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel label_1 = new JLabel("   Mot de passe   :   ");
		panel_connection_password.add(label_1);
		
		passwordField_connexion = new JPasswordField();
		passwordField_connexion.setEnabled(false);
		passwordField_connexion.setColumns(10);
		panel_connection_password.add(passwordField_connexion);
		
		JPanel panel_connection_radio_button = new JPanel();
		panel_connection.add(panel_connection_radio_button);
		panel_connection_radio_button.setLayout(new GridLayout(0, 2, 0, 0));
		
		rdbtn_connection_anon = new JRadioButton("Connexion sans compte");
		rdbtn_connection_anon.setSelected(true);
		panel_connection_radio_button.add(rdbtn_connection_anon);
		rdbtn_connection_anon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				connectionMethodChanged();
			}
		});
		
		
		rdbtn_connection_account = new JRadioButton("Connexion avec compte");
		panel_connection_radio_button.add(rdbtn_connection_account);
		rdbtn_connection_account.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				connectionMethodChanged();
			}
		});
		
		
		ButtonGroup btnGroupConnection = new ButtonGroup();
		btnGroupConnection.add(rdbtn_connection_account);
		btnGroupConnection.add(rdbtn_connection_anon);
		
		JPanel panel_connection_valid = new JPanel();
		panel_connection.add(panel_connection_valid);
		panel_connection_valid.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_4.getLayout();
		flowLayout_2.setVgap(0);
		flowLayout_2.setHgap(0);
		panel_connection_valid.add(panel_4);
		
		JButton Connecter = new JButton("Connecter");
		panel_4.add(Connecter);
		Connecter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnConnectionClicked(textField_IP.getText(), textField_Port.getText(), textField_connexion_id.getText(), new String(passwordField_connexion.getPassword()));
			}
		});
		
		JPanel panel_5 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_5.getLayout();
		flowLayout_3.setVgap(0);
		flowLayout_3.setHgap(0);
		panel_connection_valid.add(panel_5);
		
		JButton btnSpectateur = new JButton("Spectateur");
		panel_5.add(btnSpectateur);
		btnSpectateur.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnSpectateurClicked(textField_IP.getText(), textField_Port.getText());
			}
		});
		
		
		
	}
	
	
	
	
	/*********************** Fenêtres principale de jeu ***********************/
	
	public void initializeOnConnect() {
		
		frmIsketch = new JFrame();
		frmIsketch.setVisible(true);
		frmIsketch.setTitle("PySketch");
		frmIsketch.setBounds(100, 100, 1200, 850);
		frmIsketch.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				exitApplication();
			}
		});
		frmIsketch.getContentPane().setLayout(new GridLayout(1, 2, 0, 0));
		
		JPanel panel_system = new JPanel();
		panel_system.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		frmIsketch.getContentPane().add(panel_system);
		panel_system.setLayout(new GridLayout(2, 1, 0, 0));
		
		JPanel panel_game = new JPanel();
		panel_system.add(panel_game);
		panel_game.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_output_game = new JPanel();
		panel_output_game.setBorder(new TitledBorder(null, "Jeu", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_game.add(panel_output_game, BorderLayout.CENTER);
		panel_output_game.setLayout(new BorderLayout(0, 0));
		
		dlm_game = new DefaultListModel<String>();
		JList<String> jlist_game = new JList<String>(dlm_game);
		jlist_game.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		JScrollPane scrollPane = new JScrollPane(jlist_game);
		panel_output_game.add(scrollPane, BorderLayout.CENTER);
		
		JPanel panel_input_game = new JPanel();
		panel_input_game.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Jeu (entr\u00E9e)", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_game.add(panel_input_game, BorderLayout.SOUTH);
		panel_input_game.setLayout(new BorderLayout(0, 0));
		
		textField_game = new JTextField();
		panel_input_game.add(textField_game, BorderLayout.CENTER);
		textField_game.setColumns(10);
		
		btnSendGame = new JButton("Envoyer");
		btnSendGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSendGameClicked(textField_game.getText().trim());
				textField_game.setText("");
			}
		});
		panel_input_game.add(btnSendGame, BorderLayout.EAST);
		
		btnCheatGame = new JButton("Tricheur spotted !");
		btnCheatGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnTricheurClicked();
			}
		});
		panel_input_game.add(btnCheatGame, BorderLayout.WEST);
		
		JPanel panel_tchat = new JPanel();
		panel_system.add(panel_tchat);
		panel_tchat.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_output_tchat = new JPanel();
		panel_output_tchat.setBorder(new TitledBorder(null, "Tchat", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_tchat.add(panel_output_tchat, BorderLayout.CENTER);
		panel_output_tchat.setLayout(new BorderLayout(0, 0));
		
		dlm_tchat = new DefaultListModel<String>();
		JList<String> jlist_tchat = new JList<String>(dlm_tchat);
		jlist_tchat.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//panel_output_tchat.add(jlist_tchat, BorderLayout.CENTER);
		
		JScrollPane scrollPane_1 = new JScrollPane(jlist_tchat);
		panel_output_tchat.add(scrollPane_1, BorderLayout.CENTER);
		
		JPanel panel_input_tchat = new JPanel();
		panel_input_tchat.setBorder(new TitledBorder(null, "Tchat (entr\u00E9e)", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_tchat.add(panel_input_tchat, BorderLayout.SOUTH);
		panel_input_tchat.setLayout(new BorderLayout(0, 0));
		
		textField_tchat = new JTextField();
		panel_input_tchat.add(textField_tchat, BorderLayout.CENTER);
		textField_tchat.setColumns(10);
		
		btnSendTchat = new JButton("Envoyer");
		btnSendTchat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSendTchatClicked(textField_tchat.getText().trim());
				textField_tchat.setText("");
			}
		});
		panel_input_tchat.add(btnSendTchat, BorderLayout.EAST);
		
		JPanel panel_draw = new JPanel();
		panel_draw.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		frmIsketch.getContentPane().add(panel_draw);
		panel_draw.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_options = new JPanel();
		panel_options.setBorder(new TitledBorder(null, "Options", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_draw.add(panel_options, BorderLayout.NORTH);
		panel_options.setLayout(new BorderLayout(10, 0));
		
		JPanel panel_round_info = new JPanel();
		panel_options.add(panel_round_info, BorderLayout.WEST);
		panel_round_info.setLayout(new GridLayout(2, 1, 0, 0));
		
		lbl_word_to_find = new JLabel("Mot a dessiner");
		lbl_word_to_find.setHorizontalAlignment(SwingConstants.CENTER);
		panel_round_info.add(lbl_word_to_find);
		
		btn_pass = new JButton("Passer son tour");
		btn_pass.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnPassClicked();
			}
		});
		panel_round_info.add(btn_pass);
		
		JPanel panel_draw_options = new JPanel();
		panel_options.add(panel_draw_options, BorderLayout.CENTER);
		panel_draw_options.setLayout(new GridLayout(1, 2, 5, 0));
		
		ButtonGroup bGroup = new ButtonGroup();
		
		JPanel panel_shape_options = new JPanel();
		panel_draw_options.add(panel_shape_options);
		panel_shape_options.setLayout(new GridLayout(2, 1, 0, 0));
		
		JPanel panel_shape_selection = new JPanel();
		panel_shape_options.add(panel_shape_selection);
		panel_shape_selection.setLayout(new GridLayout(1, 2, 0, 0));
		
		rdbtn_line = new JRadioButton("Ligne");
		rdbtn_line.setSelected(true);
		panel_shape_selection.add(rdbtn_line);
		rdbtn_line.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				shapeSelectionChanged();
			}
		});
		
		rdbtn_bezier = new JRadioButton("Bezier");
		panel_shape_selection.add(rdbtn_bezier);
		rdbtn_bezier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				shapeSelectionChanged();
			}
		});
		bGroup.add(rdbtn_line);
		bGroup.add(rdbtn_bezier);
		
		JPanel panel_epaisseur = new JPanel();
		panel_shape_options.add(panel_epaisseur);
		panel_epaisseur.setLayout(new BorderLayout(0, 0));
		
		JLabel lblEpaisseur = new JLabel("Epaisseur : ");
		panel_epaisseur.add(lblEpaisseur, BorderLayout.WEST);
		
		slider.setPaintLabels(true);
		slider.setValue(1);
		slider.setMaximum(20);
		slider.setMinimum(1);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				epaisseurModif(slider.getValue());
			}
		});
		panel_epaisseur.add(slider, BorderLayout.CENTER);
		
		JPanel panel_color_chooser = new JPanel();
		panel_draw_options.add(panel_color_chooser);
		panel_color_chooser.setLayout(new GridLayout(2, 3, 0, 0));
		
		ButtonGroup bGroupColor = new ButtonGroup();
		
		rdbtn_color_noir = new JRadioButton("Noir");
		rdbtn_color_noir.setSelected(true);
		panel_color_chooser.add(rdbtn_color_noir);
		bGroupColor.add(rdbtn_color_noir);
		rdbtn_color_noir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorSelectionChanged();
			}
		});
		
		rdbtn_color_bleu = new JRadioButton("Bleu");
		panel_color_chooser.add(rdbtn_color_bleu);
		bGroupColor.add(rdbtn_color_bleu);
		rdbtn_color_bleu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorSelectionChanged();
			}
		});
		
		rdbtn_color_rouge = new JRadioButton("Rouge");
		panel_color_chooser.add(rdbtn_color_rouge);
		bGroupColor.add(rdbtn_color_rouge);
		rdbtn_color_rouge.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorSelectionChanged();
			}
		});
		
		rdbtn_color_vert = new JRadioButton("Vert");
		panel_color_chooser.add(rdbtn_color_vert);
		bGroupColor.add(rdbtn_color_vert);
		rdbtn_color_vert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorSelectionChanged();
			}
		});
		
		rdbtn_color_jaune = new JRadioButton("Jaune");
		panel_color_chooser.add(rdbtn_color_jaune);
		bGroupColor.add(rdbtn_color_jaune);
		rdbtn_color_jaune.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorSelectionChanged();
			}
		});
		
		rdbtn_color_gris = new JRadioButton("Gris");
		panel_color_chooser.add(rdbtn_color_gris);
		bGroupColor.add(rdbtn_color_gris);
		rdbtn_color_gris.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorSelectionChanged();
			}
		});
		
		panel_draw_in_out = new JCanvas();
		panel_draw_in_out.setBackground(Color.WHITE);
		panel_draw_in_out.setBorder(new TitledBorder(null, "Dessin", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_draw.add(panel_draw_in_out, BorderLayout.CENTER);
		panel_draw_in_out.setLayout(new BorderLayout(0, 0));
		
		canvas_event_manager = new JCanvasMouseEventListener(panel_draw_in_out, this);
		
		isGameInterfaceLaunched = true;
		
		
	}
	
	
	
	
	
	
	/*********************** Méthodes appelées par les différents events ***********************/
	
	private void btnCreateAccountClicked(String string_ip, String string_port, String pseudo, String password) {
		
		if (pseudo != null && !"".equals(pseudo) && password != null && !"".equals(password)) {
			
			pseudo = pseudo.trim();
			password = password.trim();
			
			currentPseudo = pseudo;
			connector.setIpAddress(string_ip, string_port);
			
			
			if (connector.connect()) {
				isRegistering = true;
				lblCreateMessage.setText("");
				connector.registerAccount(pseudo, password);
			}
		}
	}
	
	private void btnConnectionClicked(String string_ip, String string_port, String pseudo, String password) {
		
		if (pseudo != null && !"".equals(pseudo = pseudo.trim())) {
			
			currentPseudo = pseudo;
			connector.setIpAddress(string_ip, string_port);
			
			if (connector.connect()) {
			
				if (rdbtn_connection_anon.isSelected()) {
					connector.pseudoWanted(pseudo);
				}
				else if (rdbtn_connection_account.isSelected() && password != null && !"".equals(password = password.trim())) {
					connector.accountWanted(pseudo, password);
				}
			}
		}
	}
	
	private void connectionMethodChanged() {
		if (rdbtn_connection_account.isSelected()) {
			passwordField_connexion.setEnabled(true);
		}
		else if (rdbtn_connection_anon.isSelected()) {
			passwordField_connexion.setEnabled(false);
		}
	}
	
	private void btnSpectateurClicked(String string_ip, String string_port) {
		
		connector.setIpAddress(string_ip, string_port);
		currentPseudo = "";
		
		if (connector.connect()) {
			connector.spectatorModeWanted();
		}
	}
	
	private void btnTricheurClicked() {
		
		if (!isSpectateur && gameStarted) {
			connector.cheatSpotted(currentPseudo);
		}
	}
	
	private void btnSendGameClicked(String message_game) {
		
		if (!isSpectateur && gameStarted && message_game != null && !"".equals(message_game)) {
			connector.guessWord(message_game);
		}
	}
	
	private void btnSendTchatClicked(String message_tchat) {
		
		if (!isSpectateur && message_tchat != null && !"".equals(message_tchat)) {
			connector.sendTchatMessage(message_tchat);
		}
	}
	
	private void epaisseurModif(int epaisseur) {
		
		if (!isSpectateur && gameStarted && epaisseur < 21 && epaisseur > 0) {
			canvas_event_manager.setShapeStroke(epaisseur);
			connector.changeEpaisseur(epaisseur);
		}
	}
	
	private void btnPassClicked() {
		
		if (!isSpectateur && gameStarted) {
			connector.passerTour();
		}
	}
	
	private void exitApplication() {
		
		if (connector != null)
			connector.exit();
		
		System.exit(0);
	}
	
	private void shapeSelectionChanged() {
		if (rdbtn_line.isSelected()) {
			canvas_event_manager.setShapeNumber(1);
		}
		else if (rdbtn_bezier.isSelected()) {
			canvas_event_manager.setShapeNumber(2);
		}
	}
	
	private void colorSelectionChanged() {
		
		if (!isSpectateur && gameStarted) {
			if (rdbtn_color_noir.isSelected()) {
				colorSelected = Color.BLACK;
			}
			else if (rdbtn_color_bleu.isSelected()) {
				colorSelected = Color.BLUE;
			}
			else if (rdbtn_color_rouge.isSelected()) {
				colorSelected = Color.RED;
			}
			else if (rdbtn_color_vert.isSelected()) {
				colorSelected = Color.GREEN;
			}
			else if (rdbtn_color_jaune.isSelected()) {
				colorSelected = Color.YELLOW;
			}
			else if (rdbtn_color_gris.isSelected()) {
				colorSelected = Color.GRAY;
			}
			
			canvas_event_manager.setShapeColor(colorSelected);
			connector.changeCouleur(colorSelected.getRed(), colorSelected.getGreen(), colorSelected.getBlue());
		}
	}
	
	
	
	
	
	/*********************** Methodes de modification de l'UI ***********************/
	
	public void accessDenied() {
		
		if (isRegistering && !isGameInterfaceLaunched) {
			
			lblCreateMessage.setText("Création impossible\n");
			isRegistering = false;
		}
	}
	
	public void spectatorValidated() {
		
		frmIsketch.dispose();
		initializeOnConnect();
		
		btn_pass.setEnabled(false);
		rdbtn_color_noir.setEnabled(false);
		rdbtn_color_rouge.setEnabled(false);
		rdbtn_color_bleu.setEnabled(false);
		rdbtn_color_vert.setEnabled(false);
		rdbtn_color_jaune.setEnabled(false);
		rdbtn_color_gris.setEnabled(false);
		slider.setEnabled(false);
		rdbtn_line.setEnabled(false);
		rdbtn_bezier.setEnabled(false);
		
		lbl_word_to_find.setText("");
		
		btnSendGame.setEnabled(false);
		btnCheatGame.setEnabled(false);
		btnSendTchat.setEnabled(false);
		
		
		if (isGameInterfaceLaunched) {
			dlm_tchat.addElement("Bienvenue à toi, spectateur !");
		}
		
		frmIsketch.validate();
		isSpectateur = true;
	}
	
	public void userConnected(String pseudo) {
		
		if (isGameInterfaceLaunched) {
			dlm_tchat.addElement("L'utilisateur " + pseudo + " s'est connecte au serveur.");
		}
	}
	
	public void userExited(String pseudo) {
		
		if (isGameInterfaceLaunched) {
			dlm_tchat.addElement("L'utilisateur " + pseudo + " s'est deconnecte du serveur.");
		}
	}
	
	public void welcomeToTheGame(String pseudo) {
		
		// Lance l'interface de jeu
		frmIsketch.dispose();
		initializeOnConnect();
		frmIsketch.validate();
		
		if (isGameInterfaceLaunched) {
			dlm_tchat.addElement("Bienvenue à toi, " + pseudo + " !\n");
		}
	}
	
	public void newRound(String dessinateur_pseudo, String mot) {
		if (isGameInterfaceLaunched) {
			
			dlm_game.addElement("Un nouveau round commence ! "
					+ "Le dessinateur est : " + dessinateur_pseudo);
			
			if (!gameStarted)
				gameStarted = true;
			
			canvas_event_manager.resetDrawing();
			panel_draw_in_out.clear();
			
			if (!isSpectateur) {
				if (currentPseudo.equals(dessinateur_pseudo)) {
					
					isDessinateur = true;
					btn_pass.setEnabled(true);
					rdbtn_color_noir.setEnabled(true);
					rdbtn_color_rouge.setEnabled(true);
					rdbtn_color_bleu.setEnabled(true);
					rdbtn_color_vert.setEnabled(true);
					rdbtn_color_jaune.setEnabled(true);
					rdbtn_color_gris.setEnabled(true);
					slider.setEnabled(true);
					rdbtn_line.setEnabled(true);
					rdbtn_bezier.setEnabled(true);
					
					lbl_word_to_find.setText(mot);
					
					btnSendGame.setEnabled(false);
					btnCheatGame.setEnabled(false);
					
					connector.changeCouleur(colorSelected.getRed(), colorSelected.getGreen(), colorSelected.getBlue());
					connector.changeEpaisseur(slider.getValue());
				}
				else {
		
					isDessinateur = false;
					btn_pass.setEnabled(false);
					rdbtn_color_noir.setEnabled(false);
					rdbtn_color_rouge.setEnabled(false);
					rdbtn_color_bleu.setEnabled(false);
					rdbtn_color_vert.setEnabled(false);
					rdbtn_color_jaune.setEnabled(false);
					rdbtn_color_gris.setEnabled(false);
					slider.setEnabled(false);
					rdbtn_line.setEnabled(false);
					rdbtn_bezier.setEnabled(false);
					
					lbl_word_to_find.setText("");
					
					btnSendGame.setEnabled(true);
					btnCheatGame.setEnabled(true);
				}
			}
		}
	}
	
	public void tchatMessage(String pseudo, String message) {
		if (isGameInterfaceLaunched)
			dlm_tchat.addElement(pseudo + " : " + message);
	}
	
	public void essaiMot(String pseudo, String motFail) {
		if (isGameInterfaceLaunched)
			dlm_game.addElement(pseudo + " : " + motFail);
	}
	
	public void motTrouve(String pseudo) {
		if (isGameInterfaceLaunched) {
			dlm_game.addElement("Le joueur " + pseudo + " a trouvé le bon mot !");
			
			if (pseudo.equals(currentPseudo)) {
				btnSendGame.setEnabled(false);
				btnCheatGame.setEnabled(false);
			}
		}
	}
	
	public void finDuRound(String pseudo_winner, String mot_a_trouver) {
		if (isGameInterfaceLaunched) {
			if (mot_a_trouver != null) {
				dlm_game.addElement("Fin du round ! Le mot était : " + mot_a_trouver + " ! " + 
						(pseudo_winner != null ? ("Vainqueur : " + pseudo_winner) : ("Pas de vainqueur !")));
			}
		}
	}
	
	public void scoreRound(List<String> scores) {
		
		if (isGameInterfaceLaunched && scores != null) {
			String tmp_pseudo = null;
			StringBuilder builder = new StringBuilder();
			
			builder.append("Score du round : ");
			
			for (String tmp_str : scores) {
				if (tmp_pseudo != null) {
					builder.append(tmp_pseudo + " : " + tmp_str + " / ");
					tmp_pseudo = null;
				}
				else {
					tmp_pseudo = tmp_str;
				}
			}
			
			dlm_game.addElement(builder.toString());
		}
	}
	
	public void timeoutStarted(int timeout) {
		if (isGameInterfaceLaunched) {
			dlm_game.addElement("Plus que " + timeout + " secondes avant la fin du round !");
		}
	}
	
	public void newCourbe(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int r, int g, int b, int s) {
		
		if (isGameInterfaceLaunched) {
				
			if (r < 0 || r > 255)
				r = 0;
			if (g < 0 || g > 255)
				g = 0;
			if (b < 0 || b > 255)
				b = 0;
			
			if (s < 1)
				s = 1;
			if (s > 20)
				s = 20;
			
			panel_draw_in_out.addDrawable(new BezierDrawable(s, new Color(r, g, b), new Point(x1, y1), new Point(x2, y2), new Point(x3, y3), new Point(x4, y4)));
		}
	}
	
	public void newLine(int x1, int y1, int x2, int y2, int r, int g, int b, int s) {
		
		if (isGameInterfaceLaunched) {
				
			if (r < 0 || r > 255)
				r = 0;
			if (g < 0 || g > 255)
				g = 0;
			if (b < 0 || b > 255)
				b = 0;
			
			if (s < 1)
				s = 1;
			if (s > 20)
				s = 20;
			
			panel_draw_in_out.addDrawable(new LineDrawable(s, new Color(r, g, b), new Point(x1, y1), new Point(x2, y2)));
		}
	}
}
